package openGL.ES;

import java.nio.*;
import javax.microedition.khronos.opengles.*;

public class Triangle {
	
	private FloatBuffer vertexBuffer;
	private FloatBuffer colourBuffer; 
	
	private float vertices[] = {
		100.0f, 0.0f, 0.0f,
		200.0f, 200.0f, 0.0f,
		0.0f, 200.0f, 0.0f
	};
	
	private float colours[] = {
		1.0f, 0.0f, 0.0f, 1.0f,	// Red
		0.0f, 1.0f, 0.0f, 1.0f,	// Blue
		0.0f, 0.0f, 1.0f, 1.0f	// Green
	};
	
	public Triangle() {
		ByteBuffer bytebuf = ByteBuffer.allocateDirect(vertices.length * 4);
		bytebuf.order(ByteOrder.nativeOrder());
		vertexBuffer = bytebuf.asFloatBuffer();
		vertexBuffer.put(vertices);
		vertexBuffer.position(0);
		
		bytebuf = ByteBuffer.allocateDirect(colours.length * 4);
		bytebuf.order(ByteOrder.nativeOrder());
		colourBuffer = bytebuf.asFloatBuffer();
		colourBuffer.put(colours);
		colourBuffer.position(0);
	}
	
	public void draw(GL10 gl) {
		gl.glFrontFace(GL10.GL_CW);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		gl.glColorPointer(4, GL10.GL_FLOAT, 0, colourBuffer);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
	}
}